package com.dhcc.ws.util;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;

import org.apache.cxf.helpers.DOMUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Test {

	public static void test4(){
	try {
        String namespace = "http://basedata.service.webservice.ws.dhcc.com/";
        String wsdlUrl = "http://127.0.0.1:8011/ws/baseDataService?wsdl";
    
        //1、创建服务(Service)
        URL url = new URL(wsdlUrl);
        QName qname = new QName(namespace,"BaseDataServiceService");
        Service service = Service.create(url, qname);
        
        //2、创建Dispatch
        //public interface Dispatch<T>extends BindingProviderDispatch 接口提供对动态调用服务端点操作的支持。javax.xml.ws.Service 接口作为创建 Dispatch 实例的工厂。 
        Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(namespace,"bdWSSoap"), SOAPMessage.class, Service.Mode.MESSAGE);
        
        //3.1、创建SOAPMessage
        SOAPMessage message = MessageFactory.newInstance().createMessage();
        SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
        SOAPBody body = envelope.getBody();
        
        //3.2、处理header信息
        SOAPHeader header = envelope.getHeader();
        /*if(header==null) header = envelope.addHeader();
        QName hname = new QName(namespace,"tns");
        header.addHeaderElement(hname).addChildElement("username").addTextNode("admin");
        header.addHeaderElement(hname).addChildElement("password").addTextNode("123456");*/
        
        
        QName usernameQName = new QName(namespace, "UsernameToken","tns");
        QName passwordQName = new QName(namespace, "PasswordText","tns");
        String username = "admin";
        String password = "123456";

        //添加SiebelUsernameToken
        SOAPHeaderElement usernameHeaderElement = header.addHeaderElement(usernameQName);
        usernameHeaderElement.addTextNode(username);
        //添加SiebelPasswordText
        SOAPHeaderElement passwordHeaderElement = header.addHeaderElement(passwordQName);
        passwordHeaderElement.addTextNode(password);
        message.saveChanges();
        //4、创建QName来指定消息中传递数据
        QName ename = new QName(namespace,"findOrg","tns");
        body.addBodyElement(ename);
        message.writeTo(System.out);
        System.out.println(); 
        
        //5、通过Dispatch传递消息,并返回响应消息
        /*SOAPMessage returnMessage = dispatch.invoke(message);
        returnMessage.writeTo(System.out);//打印返回消息
        System.out.println();*/
        
        //5、解析返回的SOAP消息的XML
        // Document doc = returnMessage.getSOAPBody().extractContentAsDocument();
        //Document doc = returnMessage.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();
        //String result = doc.getElementsByTagName("deleteResult").item(0).getTextContent();
        //System.out.println("result is :" + result);
        
    } catch (Exception e) {
        e.printStackTrace();
    }
	}
	public static void main(String[] args) {
		 test4();
	}
}
